package org.smartopen.unionpay.openapi.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.smartframework.core.api.Resp;
import org.smartframework.system.log.annotation.SysLog;
import org.smartframework.unionpay.domain.request.SmartPayMicropayRequest;
import org.smartframework.unionpay.domain.request.SmartPayQueryRequest;
import org.smartframework.unionpay.domain.request.SmartPayRefundRequest;
import org.smartframework.unionpay.domain.request.SmartPayUnifiedorderRequest;
import org.smartframework.unionpay.domain.result.SmartPayMicropayResult;
import org.smartframework.unionpay.domain.result.SmartPayQueryResult;
import org.smartframework.unionpay.domain.result.SmartPayRefundResult;
import org.smartframework.unionpay.domain.result.SmartPayUnifiedorderResult;
import org.smartframework.unionpay.service.SmartUnionPayService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@Api(tags = {"支付接口"})
@RestController
@RequiredArgsConstructor
public class UnionPayOpenApiTranController {

    private final SmartUnionPayService unionPayService;

    /**
     * 统一支付
     * @param request
     * @return
     */
    @PostMapping("/tran/unifiedorder")
    @ApiOperation("统一支付")
    @SysLog("统一支付")
    public Resp<SmartPayUnifiedorderResult> unifiedorder(@Valid@RequestBody SmartPayUnifiedorderRequest request) {
        return Resp.ok(unionPayService.unifiedorder(request));
    }
    /**
     * 统一支付
     * @param request
     * @return
     */
    @PostMapping("/tran/micropay")
    @ApiOperation("付款码支付")
    @SysLog("付款码支付")
    public Resp<SmartPayMicropayResult> micropay(@Valid @RequestBody SmartPayMicropayRequest request) {
        return Resp.ok(unionPayService.micropay(request));
    }
    /**
     * 订单查询
     *
     * @param request
     * @return
     */
    @PostMapping("/tran/query")
    @ApiOperation("订单查询")
    @SysLog("订单查询")
    public Resp<SmartPayQueryResult> queryOrder(@Valid@RequestBody SmartPayQueryRequest request) {
        return Resp.ok(unionPayService.queryOrder(request));
    }

    /**
     * 订单退款
     *
     * @param request
     * @return
     */
    @PostMapping("/tran/refund")
    @ApiOperation("订单退款")
    @SysLog("订单退款")
    public Resp<SmartPayRefundResult> refundOrder(@Valid@RequestBody SmartPayRefundRequest request) {
        return Resp.ok(unionPayService.refundOrder(request));
    }
}
